package com.sinocare.project.store.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sinocare.project.common.domain.SccDrugstore;
import com.sinocare.project.common.domain.Store;
import com.sinocare.project.common.domain.vo.StoreVo;
import com.sinocare.project.common.result.Result;
import com.sinocare.project.common.result.ResultCode;
import com.sinocare.project.store.service.SccDrugStoreService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("drugstore")
public class SccDrugStoreController {

    @Autowired
    private SccDrugStoreService sccDrugStoreService;

    //   添加分店
    @ApiOperation(value = "添加分店",notes = "新增分店",httpMethod = "POST")
    @RequestMapping(value = "add",method = RequestMethod.POST)
    public Result addstore(@RequestParam(value = "storeNameCode") String storecode, SccDrugstore sccDrugstore) throws Exception{

        sccDrugstore.setStoreCode(storecode+"_"+sccDrugstore.getStoreCode());

        boolean save = sccDrugStoreService.save(sccDrugstore);

        if (save){
            return Result.ok(ResultCode.SUCCESS,1);
        }else {
            return Result.fail(ResultCode.FAIL,null);
        }

    }


    //   添加总店
    @ApiOperation(value = "添加总店",notes = "新增总店",httpMethod = "POST")
    @RequestMapping(value = "addstore",method = RequestMethod.POST)
    public Result addDrugstore(SccDrugstore sccDrugstore) throws Exception{


        boolean save = sccDrugStoreService.save(sccDrugstore);

        if (save){
            return Result.ok(ResultCode.SUCCESS,1);
        }else {
            return Result.fail(ResultCode.FAIL,null);
        }

    }





    @ApiOperation(value = "根据id批量删除分店",notes = "根据id批量删除分店",httpMethod = "DELETE")
    @RequestMapping(value = "deleteStore",method = RequestMethod.DELETE)
    public Result deleteStores(@RequestParam(value = "ids") String ids)throws Exception{

        System.out.println(ids);
        ids=ids.substring(1);
        String[] split = ids.split(",");
        int i = sccDrugStoreService.deleteStores(split);
        if (i>0){
            return Result.ok(ResultCode.SUCCESS,i);
        }else {
            return Result.fail(ResultCode.FAIL,null);
        }

    }

}
